package com.zqq.goods.ui.adapter

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.eightbitlab.rxbus.Bus
import com.zqq.baselibrary.ext.loadUrl
import com.zqq.baselibrary.ext.onClick
import com.zqq.baselibrary.ui.adapter.BaseRecyclerViewAdapter
import com.zqq.baselibrary.utils.YuanFenConverter
import com.zqq.baselibrary.widgets.DefaultTextWatcher
import com.zqq.goods.R
import com.zqq.goods.event.CartAllCheckedEvent
import com.zqq.goods.event.UpdateTotalPriceEvent
import com.zqq.goods.getEditText
import com.zqq.goods.service.bean.CartGoods
import kotlinx.android.synthetic.main.layout_cart_goods_item.view.*
import org.greenrobot.eventbus.EventBus

/**
 * 购物车数据适配器
 * wc
 * @author zqq on 2019/6/5.
 */
class CartGoodsAdapter(context: Context) : BaseRecyclerViewAdapter<CartGoods, CartGoodsAdapter.ViewHolder>(context) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(mContext).inflate(R.layout.layout_cart_goods_item, parent, false)
        return ViewHolder(view)
    }


    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        super.onBindViewHolder(holder, position)
        val model = dataList[position]
        //是否选中
        holder.itemView.mCheckedCb.isChecked = model.isSelected
        //加载商品图片
        holder.itemView.mGoodsIconIv.loadUrl(model.goodsIcon)
        //商品描述
        holder.itemView.mGoodsDescTv.text = model.goodsDesc
        //商品SKU
        holder.itemView.mGoodsSkuTv.text = model.goodsSku
        //商品价格
        holder.itemView.mGoodsPriceTv.text = YuanFenConverter.changeF2YWithUnit(model.goodsPrice)

        //商品数量
        holder.itemView.mGoodsCountBtn.setCurrentNumber(model.goodsCount)

        //Check事件
        holder.itemView.mCheckedCb.onClick {
            model.isSelected = holder.itemView.mCheckedCb.isChecked

            //每点击一次就要判断所有选项是否被选中
            //all方法：判断dataList里所有的it是否是某个状态
            //即若所有it.isSelected如果是true，则返回true即isAllChecked=true
            val isAllChecked = dataList.all { it.isSelected }

            Bus.send(CartAllCheckedEvent(isAllChecked))
//            EventBus.getDefault().post(CartAllCheckedEvent(isAllChecked))
            notifyDataSetChanged()
        }

        //商品数量变化监听(更新价格)
        holder.itemView.mGoodsCountBtn.getEditText()
                .addTextChangedListener(object : DefaultTextWatcher() {
                    override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                        if (!TextUtils.isEmpty(s.toString())) {
                            model.goodsCount = s.toString().toInt()
                        }

                        Bus.send(UpdateTotalPriceEvent())
//                        EventBus.getDefault().post(UpdateTotalPriceEvent())
                    }
                })
    }


    class ViewHolder(view: View) : RecyclerView.ViewHolder(view)


}